package com.ruoyi.web.controller.parts;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.CollectionUtils;
import com.ruoyi.system.domain.ProductType;
import com.ruoyi.system.domain.vo.productInfo.ProductDictVo;
import com.ruoyi.system.domain.vo.productInfo.ProductInfoListVo;
import com.ruoyi.system.service.IProductTypeService;
import com.ruoyi.system.service.impl.ProductTypeServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.ProductInfo;
import com.ruoyi.system.service.IProductInfoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 产品信息Controller
 *
 * @author ruoyi
 * @date 2023-01-25
 */
@RestController
@RequestMapping("/parts/productInfo")
public class ProductInfoController extends BaseController
{
    @Autowired
    private IProductInfoService productInfoService;

    @Autowired
    private IProductTypeService productTypeService;

    /**
     * 查询产品信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:productinfo:list')")
    @GetMapping("/list")
    public TableDataInfo list(ProductInfo productInfo)
    {
        startPage();
        List<ProductInfo> list = productInfoService.selectProductInfoList(productInfo);
        TableDataInfo dataTable = getDataTable(list);
        List<?> rows = dataTable.getRows();
        List<ProductInfoListVo> voList = CollectionUtils.convertCollectionType(rows, ProductInfoListVo.class);
        Map<Long, ProductType> typeMap = productTypeService.selectProductTypeMap(null);
        voList = voList.stream().map((vo)->{
            Long id = vo.getProductTypeId();
            String name = typeMap.get(id).getName();
            vo.setProductTypeStr(name);
            return vo;
        }).collect(Collectors.toList());
        dataTable.setRows(voList);
        return dataTable;
    }


    /**
     * 查询产品信息列表dict
     */
    @PreAuthorize("@ss.hasPermi('system:productinfo:list')")
    @GetMapping("/getProductDict")
    public TableDataInfo getProductDict(ProductInfo productInfo)
    {
//        startPage();
        List<ProductDictVo> list = productInfoService.selectProductDict(productInfo);
        return getDataTable(list);
    }

    /**
     * 导出产品信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:productinfo:export')")
    @Log(title = "产品信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ProductInfo productInfo)
    {
        List<ProductInfo> list = productInfoService.selectProductInfoList(productInfo);
        ExcelUtil<ProductInfo> util = new ExcelUtil<ProductInfo>(ProductInfo.class);
        util.exportExcel(response, list, "产品信息数据");
    }

    /**
     * 获取产品信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:productinfo:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(productInfoService.selectProductInfoById(id));
    }

    /**
     * 新增产品信息
     */
    @PreAuthorize("@ss.hasPermi('system:productinfo:add')")
    @Log(title = "产品信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProductInfo productInfo)
    {
        return toAjax(productInfoService.insertProductInfo(productInfo));
    }

    /**
     * 修改产品信息
     */
    @PreAuthorize("@ss.hasPermi('system:productinfo:edit')")
    @Log(title = "产品信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProductInfo productInfo)
    {
        return toAjax(productInfoService.updateProductInfo(productInfo));
    }

    /**
     * 删除产品信息
     */
    @PreAuthorize("@ss.hasPermi('system:productinfo:remove')")
    @Log(title = "产品信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(productInfoService.deleteProductByIds(ids));
    }
}
